package com.jhgsys.internal.hazard.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jhgsys.internal.hazard.entity.HazardReportFile;
import com.jhgsys.internal.hazard.mapper.HazardReportFileMapper;
import com.jhgsys.internal.hazard.service.IHazardReportFileService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 隐患报告附件 Service实现
 *
 * @author wanghw
 * @date 2024-08-20 09:23:07
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HazardReportFileServiceImpl extends ServiceImpl<HazardReportFileMapper, HazardReportFile> implements IHazardReportFileService {

    /**
     * 查询（分页）
     * @author wanghw
     * @date 2024-08-20 09:23:07
     * @param request QueryRequest
     * @param hazardReportFile hazardReportFile
     * @return IPage<HazardReportFile>
    */
    @Override
    public IPage<HazardReportFile> findHazardReportFilePage(QueryRequest request, HazardReportFile hazardReportFile) {
        LambdaQueryWrapper<HazardReportFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardReportFile::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<HazardReportFile> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author wanghw
     * @date 2024-08-20 09:23:07
     * @param hazardReportFile hazardReportFile
     * @return List<HazardReportFile>
     */
    @Override
    public List<HazardReportFile> findHazardReportFileList(HazardReportFile hazardReportFile) {
	    LambdaQueryWrapper<HazardReportFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardReportFile::getDeletedFlg, 0);
		if(StringUtils.isNotBlank(hazardReportFile.getReportId())){
            if(hazardReportFile.getReportId().indexOf(StringPool.COMMA) > -1){
                List<String> list = Arrays.asList(hazardReportFile.getReportId().split(StringPool.COMMA));
                queryWrapper.in(HazardReportFile::getReportId,list);
            }else {
                queryWrapper.eq(HazardReportFile::getReportId,hazardReportFile.getReportId());
            }

        }
        if(StringUtils.isNotBlank(hazardReportFile.getDeletedFlg())){
            queryWrapper.eq(HazardReportFile::getDeletedFlg,hazardReportFile.getDeletedFlg());
        }
        if(StringUtils.isNotBlank(hazardReportFile.getReportFileType())){
            queryWrapper.eq(HazardReportFile::getReportFileType,hazardReportFile.getReportFileType());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author wanghw
    * @date 2024-08-20 09:23:07
    * @param id
    * @return HazardReportFile
    */
    @Override
    public HazardReportFile findHazardReportFileById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author wanghw
     * @date 2024-08-20 09:23:07
     * @param hazardReportFile hazardReportFile
     */
    @Override
    @Transactional
    public void createHazardReportFile(HazardReportFile hazardReportFile) {
        hazardReportFile.setCreatedTime(new Date());
        this.save(hazardReportFile);
    }

    /**
     * 修改
     * @author wanghw
     * @date 2024-08-20 09:23:07
     * @param hazardReportFile hazardReportFile
     */
    @Override
    @Transactional
    public void updateHazardReportFile(HazardReportFile hazardReportFile) {
        hazardReportFile.setUpdatedTime(new Date());
        this.saveOrUpdate(hazardReportFile);
    }

    /**
     * 逻辑删除
     * @author wanghw
     * @date 2024-08-20 09:23:07
     * @param hazardReportFile hazardReportFile
     * @param hazardReportFileIds hazardReportFileIds
     */
    @Override
    @Transactional
    public void deleteHazardReportFile(HazardReportFile hazardReportFile,String hazardReportFileIds) {
        List<String> list = Arrays.asList(hazardReportFileIds.split(StringPool.COMMA));
        this.baseMapper.update(hazardReportFile,new LambdaQueryWrapper<HazardReportFile>().in(HazardReportFile::getReportFileId,list));
	}

    /**
    * 物理删除
    * @author wanghw
    * @date 2024-08-20 09:23:07
    * @param hazardReportFileIds hazardReportFileIds
    */
    @Override
    @Transactional
    public void physicallyDeleteHazardReportFile(String hazardReportFileIds) {
        List<String> list = Arrays.asList(hazardReportFileIds.split(StringPool.COMMA));
        LambdaQueryWrapper<HazardReportFile> wapper = new LambdaQueryWrapper<>();
        wapper.in(HazardReportFile::getReportFileId,list);
        this.remove(wapper);
    }
}
